<?php

require_once APPPATH."/controllers/BasePriv.php";
require APPPATH."/libraries/FFmpeg.php";
require_once APPPATH."/libraries/Tools.php";
use Aws\S3\S3Client;
class Material_bak extends BasePriv
{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', "url"));
        $this->load->model("Host_material_model", "table");
    }
//////////////////////////////////////////////////////////////////原始素材开始/////////////////////////////////////////////////////////////////////////////
    /**
     * 原始素材主页
     */
    public function index(){
        //分页
        $arr=[];
        $arr['page']=intval($this->get('page'))>0 ? intval($this->get('page')) : 1;
        $arr['limit']=intval($this->get('limit'))>0 ? intval($this->get('limit')) : 10;
        //分类筛选
        if(!empty($_GET['join_type_sub'])){
            $arr['join_type_sub']=$_GET['join_type_sub'];
        }
        //时间搜索
        if(!empty($_GET['time'])) {
            $get_time=$this->getStartAndEndTime("get");
            $start_time=strtotime($get_time[0]);
            $end_times=strtotime($get_time[1]);
            $end_time=$end_times+86400;
            $start=date("Y-m-d H:i:s",$start_time);
            $end=date("Y-m-d H:i:s",$end_time);
            $arr['start_time'] =$start;
            $arr['end_time'] =$end;
        }
        $arr['type']=2;
        //查询数据（原始素材）
        $res=$this->table->all($arr);
        //查询分类
        $type=$this->table->types(2);
        //查询话题
        $type_sub=$this->table->type_sub("",3);
        //显示话题和分类
        if(!empty($res['row'])){
            foreach($res['row'] as $k=>&$v){
                foreach($type as $kk=>$vv){
                    if($v['video_join_type']==$vv['id']){
                        $v['video_join_type_name']=$vv['name'];
                    }
                }
                foreach($type_sub as $kkk=>$vvv){
                    if($v['video_join_sub']==$vvv['id']){
                        $v['video_join_sub_name']=$vvv['sub_title'];
                    }
                }
            }
        }

        //把数据带入视图
        $data['type']=1;
        $data['data']=$res;
        $data['page']=$arr['page'];
        $data['limit']=$arr['limit'];
        $data['type_sub']=$type_sub;
        $this->layout("material/material/index",$data);
    }

    /**
     * 添加原始素材
     */
    public function add_material(){
        $this->layout("/material/material/add_material");
    }

    /**
     * 保存原始素材
     */
    public function save_material(){
//        var_dump($_POST);exit;
        if(empty($_POST['type'])){
            response(-1,"没有选择分类");
        }
        if(empty($_POST['type_sub'])){
            response(-1,"没有选择话题");
        }
        if(empty($_POST['video_title'])){
            response(-1,"没有视频名称");
        }
        if(empty($_POST['video_url1'])){
            response(-1,"没有添加完整推荐视频");
        }
        if(empty($_POST['video_url2'])){
            response(-1,"没有添加无声视频");
        }
        if(empty($_POST['video_url3'])){
            response(-1,"没有添加背景音乐");
        }
        if(empty($_POST['video_url4'])){
            response(-1,"没有添加横屏封面");
        }
        if(!empty($_POST['lines'])){
            foreach($_POST['lines'] as $k=>$v){
                foreach($v as $kk=>$vv){
                    if(mb_strlen($vv)>20){
                        response(-1,"已超过最大20字,配音文字");
                    }
                }
            }
            $data['video_lines']=json_encode($_POST['lines']);
        }
        if(mb_strlen($_POST['video_title'])>30){
            response(-1,"已超过最大30字");
        }
        //获取视屏时长
        $ffmpeg = new FFmpeg(false);

        if(empty($_POST['video_url5'])){
            //用ffmpeg截取视频第一帧作为封面
            $image_dir=$this->upload_dir();
            $true=$ffmpeg->getCoverFromVideo($_POST['video_url1'],$image_dir['uploadpath']);
            if($true){
                $data['video_small_pic']=$image_dir['savehttp'];
            }
        }else{
            $data['video_small_pic']=$_POST['video_url5'];
        }

        //获取视频和音频时长
        $data['video_time']=$ffmpeg->getVideoLen($_POST['video_url1']);
        $data['music_time']=$ffmpeg->getVideoLen($_POST['video_url3']);
        //获取视频封面第一帧
        $image_dir1=$this->upload_dir();
        $true=$ffmpeg->getCoverFromVideo($_POST['video_url1'],$image_dir1['uploadpath']);
        $data['first_pic1']=$image_dir1['savehttp'];

        $key1=rand(1000,10000);
        $key2=rand(10000,20000);
        $key3=rand(1000,10000);
        $key4=rand();
        $key=$key1.$key2.$key3.$key4;
        $data['video_uuid']=$key;
        $data['video_desc']=$_POST['video_title'];
        $data['video_join_type']=$_POST['type'];
        $data['video_join_sub']=$_POST['type_sub'];
        $data['video_url']=$_POST['video_url1'];
        $data['video_no_music_url']=$_POST['video_url2'];
        $data['video_music_url']=$_POST['video_url3'];
        $data['video_big_pic']=$_POST['video_url4'];
        $data['video_type']=2;
        $data['add_time']=date("Y-m-d H:i:s",time());
        $data['update_time']=date("Y-m-d H:i:s",time());
        $res=$this->table->add("video",$data);
        if($res){
            response(0,"添加成功",base_url("material/material/view_material?video_uuid={$key}"));
        }else{
            response(-1,"添加失败");
        }

    }

    /**
     * 原始素材直接加入挑选视频
     */
    public function save_material_join(){
//        var_dump($_POST);exit;
        if(empty($_POST['type'])){
            response(-1,"没有选择分类");
        }
        if(empty($_POST['type_sub'])){
            response(-1,"没有选择话题");
        }
        if(empty($_POST['video_title'])){
            response(-1,"没有视频名称");
        }
        if(empty($_POST['video_url1'])){
            response(-1,"没有添加完整推荐视频");
        }
        if(empty($_POST['video_url2'])){
            response(-1,"没有添加无声视频");
        }
        if(empty($_POST['video_url3'])){
            response(-1,"没有添加背景音乐");
        }
        if(empty($_POST['video_url4'])){
            response(-1,"没有添加横屏封面");
        }
        if(mb_strlen($_POST['video_title'])>30){
            response(-1,"已超过最大30字");
        }
        $ffmpeg = new FFmpeg(false);

        if(empty($_POST['video_url5'])){
            //用ffmpeg截取视频第一帧作为封面
            $image_dir=$this->upload_dir();
            $true=$ffmpeg->getCoverFromVideo($_POST['video_url1'],$image_dir['uploadpath']);
            if($true){
                $data['video_small_pic']=$image_dir['savehttp'];
            }
        }else{
            $data['video_small_pic']=$_POST['video_url5'];
        }
        //获取视频和音频时长
        $data['video_time']=$ffmpeg->getVideoLen($_POST['video_url1']);
        $data['music_time']=$ffmpeg->getVideoLen($_POST['video_url3']);
        //获取视频封面第一帧
        $image_dir1=$this->upload_dir();
        $true=$ffmpeg->getCoverFromVideo($_POST['video_url1'],$image_dir1['uploadpath']);
        $data['first_pic1']=$image_dir1['savehttp'];

        if(!empty($_POST['lines'])){
            foreach($_POST['lines'] as $k=>$v){
                foreach($v as $kk=>$vv){
                    if(mb_strlen($vv)>20){
                        response(-1,"已超过最大20字,配音文字");
                    }
                }
            }
            $data['video_lines']=json_encode($_POST['lines']);
        }
        $key1=rand(1000,10000);
        $key2=rand(10000,20000);
        $key3=rand(1000,10000);
        $key4=rand();
        $key=$key1.$key2.$key3.$key4;
        $data['video_uuid']=$key;
        $data['video_desc']=$_POST['video_title'];
        $data['video_join_type']=$_POST['type'];
        $data['video_join_sub']=$_POST['type_sub'];
        $data['video_url']=$_POST['video_url1'];
        $data['video_no_music_url']=$_POST['video_url2'];
        $data['video_music_url']=$_POST['video_url3'];
        $data['video_big_pic']=$_POST['video_url4'];
        $data['video_type']=3;
        $data['add_time']=date("Y-m-d H:i:s",time());
        $data['join_time']=date("Y-m-d H:i:s",time());
        $data['update_time']=date("Y-m-d H:i:s",time());
        $res=$this->table->add("video",$data);
        if($res){
            response(0,"添加成功",base_url("material/material/view_material?video_uuid={$key}"));
        }else{
            response(-1,"添加失败");
        }

    }

//////////////////////////////////////////////////////////////////原始素材结束/////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////已挑选视频开始/////////////////////////////////////////////////////////////////////////////
    /**
     * 已挑选视频主页
     */
    public function join(){
        //分页
        $arr=[];
        $arr['page']=intval($this->get('page'))>0 ? intval($this->get('page')) : 1;
        $arr['limit']=intval($this->get('limit'))>0 ? intval($this->get('limit')) : 10;
        //分类筛选
        if(!empty($_GET['join_type_sub'])){
            $arr['join_type_sub']=$_GET['join_type_sub'];
        }
        //时间搜索
        if(!empty($_GET['time'])) {
            $get_time=$this->getStartAndEndTime("get");
            $start_time=strtotime($get_time[0]);
            $end_times=strtotime($get_time[1]);
            $end_time=$end_times+86400;
            $start=date("Y-m-d H:i:s",$start_time);
            $end=date("Y-m-d H:i:s",$end_time);
            $arr['start_time'] =$start;
            $arr['end_time'] =$end;
        }
        //排序值
        if(!empty($_GET['sort'])){
            $arr['sort']=$_GET['sort'];
            $data['sort']=1;
        }
        //配音次数
        if(!empty($_GET['dub_num'])){
            $arr['dub_num']=$_GET['dub_num'];
            $data['dub_num']=1;
        }
        //时间
        if(!empty($_GET['times'])){
            $arr['times']=$_GET['times'];
            $data['times']=1;
        }
        //查询数据（原始素材）
        $res=$this->table->join($arr);
        //查询分类
        $type=$this->table->types(2);
        //查询话题
        $type_sub=$this->table->type_sub("",3);
        //显示话题和分类
        if(!empty($res['row'])){
            foreach($res['row'] as $k=>&$v){
                foreach($type as $kk=>$vv){
                    if($v['video_join_type']==$vv['id']){
                        $v['video_join_type_name']=$vv['name'];
                    }
                }
                foreach($type_sub as $kkk=>$vvv){
                    if($v['video_join_sub']==$vvv['id']){
                        $v['video_join_sub_name']=$vvv['sub_title'];
                    }
                }
            }
        }

        //把数据带入视图
        $data['type']=2;
        $data['data']=$res;
        $data['page']=$arr['page'];
        $data['limit']=$arr['limit'];
        $data['type_sub']=$type_sub;
        $this->layout("material/material/join",$data);
    }

    /**
     * 已挑选视频排序
     */
    public function paixu(){
        $video_uuid=$_POST['video_uuid'];
        if(empty($video_uuid)){
            response(-1,"参数不能为空");
        }
        $res=$this->table->one("video",["video_uuid"=>$video_uuid]);
//        if($res['sorts']==999999999 || empty($res['sorts'])){
//            $order=0;
//        }else{
//            $order=$res['sorts'];
//        }
        $order=$res['sorts'];
        if($res){
            $html="";
            $html.="<br>";
            $html.="<div class=\"layui-form-item\">";
            $html.="<label class=\"layui-form-label\">当前排序</label>";
            $html.="<div class=\"layui-input-block\">";
            $html.="<input type=\"text\" name=\"title\" value=\"{$order}\" placeholder=\"请输入排序\" class=\"layui-input\" disabled='disabled'>";
            $html.="</div>";
            $html.="</div>";
            $html.="<div class=\"layui-form-item\">";
            $html.="<label class=\"layui-form-label\">更改排序</label>";
            $html.="<div class=\"layui-input-block\">";
            $html.="<input type=\"text\" name=\"\" placeholder=\"请输入排序\" class=\"layui-input edit_paixu\">";
            $html.="</div>";
            $html.="</div>";
            response(0,"成功",$html);
        }else{
            response(-1,"失败");
        }
    }

    /**
     * 保存排序
     */
    public function edit_paixu(){
//        var_dump($_POST['paixu']);exit;
        if(!is_numeric($_POST['paixu'])){
            response(-1,"排序值不是数字");
        }
        if($_POST['paixu']<0 || $_POST['paixu']>999){
            response(-1,"排序值不合法");
        }
        if(empty($_POST['video_uuid'])){
            response(-1,"参数不能为空");
        }
        if(empty($_POST['paixu']) || $_POST['paixu']==0){
            $_POST['paixu']=0;
        }
        $old_sort=$this->table->one("video",['video_uuid'=>$_POST['video_uuid']],"sorts");

        $res=$this->table->update_sort($_POST['paixu'],$_POST['video_uuid'],$old_sort['sorts']);
        if($res){
            response(0,"排序成功");
        }else{
            response(-1,"排序失败");
        }
    }

//////////////////////////////////////////////////////////////////已挑选视频开始/////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////弃用视频开始/////////////////////////////////////////////////////////////////////////////
    /**
     * 弃用视频主页
     */
    public function drop(){
        //分页
        $arr=[];
        $arr['page']=intval($this->get('page'))>0 ? intval($this->get('page')) : 1;
        $arr['limit']=intval($this->get('limit'))>0 ? intval($this->get('limit')) : 10;
        $arr['type']=1;
        //分类筛选
        if(!empty($_GET['join_type_sub'])){
            $arr['join_type_sub']=$_GET['join_type_sub'];
        }
        //时间搜索
        if(!empty($_GET['time'])) {
            $get_time=$this->getStartAndEndTime("get");
            $start_time=strtotime($get_time[0]);
            $end_times=strtotime($get_time[1]);
            $end_time=$end_times+86400;
            $start=date("Y-m-d H:i:s",$start_time);
            $end=date("Y-m-d H:i:s",$end_time);
            $arr['start_time'] =$start;
            $arr['end_time'] =$end;
        }
        //查询数据（原始素材）
        $res=$this->table->all($arr);
        //查询分类
        $type=$this->table->types(2);
        //查询话题
        $type_sub=$this->table->type_sub("",3);
        //显示话题和分类
        if(!empty($res['row'])){
            foreach($res['row'] as $k=>&$v){
                foreach($type as $kk=>$vv){
                    if($v['video_join_type']==$vv['id']){
                        $v['video_join_type_name']=$vv['name'];
                    }
                }
                foreach($type_sub as $kkk=>$vvv){
                    if($v['video_join_sub']==$vvv['id']){
                        $v['video_join_sub_name']=$vvv['sub_title'];
                    }
                }
            }
        }

        //把数据带入视图
        $data['type']=3;
        $data['data']=$res;
        $data['page']=$arr['page'];
        $data['limit']=$arr['limit'];
        $data['type_sub']=$type_sub;
        $this->layout("material/material/drop",$data);
    }
//////////////////////////////////////////////////////////////////弃用视频开始/////////////////////////////////////////////////////////////////////////////

    /**
     * 编辑素材
     */
    public function view_material(){
        $video_uuid=$_REQUEST['video_uuid'];
        if(empty($video_uuid)){
            response(-1,"参数不能为空");
        }
        //根据视频uuid查询出视频信息
        $res=$this->table->one("video",["video_uuid"=>$video_uuid]);
        $data['data']=$res;
        $data['types']=$this->table->types();
        $data['type_sub']=$this->table->type_sub(['sub_type'=>$res['video_join_type']]);
        $this->layout("material/material/view_material",$data);
    }

    /**
     * 保存素材编辑
     */
    public function edit_material(){
        if(empty($_POST['type'])){
            response(-1,"没有选择分类");
        }
        if(empty($_POST['type_sub'])){
            response(-1,"没有选择话题");
        }
        if(empty($_POST['video_desc'])){
            response(-1,"没有视频名称");
        }
        if(empty($_POST['video_url1'])){
            response(-1,"没有添加完整推荐视频");
        }
        if(empty($_POST['video_url2'])){
            response(-1,"没有添加无声视频");
        }
        if(empty($_POST['video_url3'])){
            response(-1,"没有添加背景音乐");
        }
        if(empty($_POST['video_url4'])){
            response(-1,"没有添加横屏封面");
        }
        if(empty($_POST['video_url5'])){
            response(-1,"没有添加小封面图");
        }
        if(!empty($_POST['lines'])){
            foreach($_POST['lines'] as $k=>$v){
                foreach($v as $kk=>$vv){
                    if(mb_strlen($vv)>20){
                        response(-1,"已超过最大20字,配音文字");
                    }
                }
            }
            $data['video_lines']=json_encode($_POST['lines']);
        }else{
            $data['video_lines']="";
        }
        if(mb_strlen($_POST['video_desc'])>30){
            response(-1,"已超过最大30字");
        }
        //先去查询这个视频的数据
        $r1=$this->table->one("video",["video_uuid"=>$_POST['video_uuid']]);
        $data['video_desc']=$_POST['video_desc'];
        $data['video_join_type']=$_POST['type'];
        $data['video_join_sub']=$_POST['type_sub'];
        $data['video_big_pic']=$_POST['video_url4'];
        $data['video_small_pic']=$_POST['video_url5'];
        $data['video_url']=$_POST['video_url1'];
        $data['video_no_music_url']=$_POST['video_url2'];
        $data['video_music_url']=$_POST['video_url3'];
        $data['update_time']=date("Y-m-d H:i:s",time());
        $data['transcode_status']=0;
        $data['transcode_count']=0;
        $data['transcode_msg']="";

        //用ffmpeg
        $ffmpeg = new FFmpeg(false);
        if($data['video_url'] != $r1['video_url']){
            $data['transcode_video_url']="";
            $data['width']="";
            $data['height']="";
            $data['video_time']=$ffmpeg->getVideoLen($data['video_url']);
            //获取视频封面第一帧
            $image_dir1=$this->upload_dir();
            $true=$ffmpeg->getCoverFromVideo($data['video_url'],$image_dir1['uploadpath']);
            $data['first_pic1']=$image_dir1['savehttp'];
        }
        if($data['video_no_music_url'] != $r1['video_no_music_url']){
            $data['transcode_video_no_music_url']="";
        }
        if($data['video_music_url'] != $r1['video_music_url']){
            $data['cut_music_url']="";
            $data['music_time']=$ffmpeg->getVideoLen($_POST['video_url3']);
        }
        if($data['video_music_url'] != $r1['video_music_url'] || $data['video_url'] != $r1['video_url'] || $data['video_no_music_url'] != $r1['video_no_music_url']){
            $data['is_update']=2;
        }
//        var_dump($data);exit;
        $res=$this->table->update("video",$data,["video_uuid"=>$_POST['video_uuid']]);
        if($res){
            if($_POST['video_type']==3){
                response(0,"修改成功",base_url("material/material/join"));
            }else if($_POST['video_type']==1){
                response(0,"修改成功",base_url("material/material/drop"));
            }else{
                response(0,"修改成功",base_url("material/material/index"));
            }
        }else{
            response(-1,"修改失败");
        }
    }

    /**
     * 把素材添加为已挑选视频
     */
    public function join_make_material(){
        $video_uuid=$_POST['video_uuid'];
        if(empty($video_uuid)){
            response(-1,"缺少参数");
        }
        $data['video_type']=3;
        $data['join_time']=date("Y-m-d H:i:s",time());
        $res=$this->table->update("video",$data,["video_uuid"=>$video_uuid]);
        if($res){
            response(0,"加入成功");
        }else{
            response(-1,"加入失败");
        }
    }

    /**
     * 把素材添加为原始素材
     */
    public function join_make_materials(){
        $video_uuid=$_POST['video_uuid'];
        if(empty($video_uuid)){
            response(-1,"缺少参数");
        }
        $data['video_type']=2;
        $data['join_time']=date("Y-m-d H:i:s",time());
        $res=$this->table->update("video",$data,["video_uuid"=>$video_uuid]);
        if($res){
            response(0,"加入成功");
        }else{
            response(-1,"加入失败");
        }
    }

    /**
     * 弃用
     */
    public function drop_make_material(){
        $video_uuid=$_POST['video_uuid'];
        if(empty($video_uuid)){
            response(-1,"缺少参数");
        }
        //1.先去查询要弃用素材信息
//        $da1=$this->table->one("video",['video_uuid'=>$video_uuid]);
//        var_dump($da1['video_type']);
//        var_dump($da1['sorts']);
//        exit;
        //2.如果是挑选视频，把这个视频的排序先改为999999999，再去把大于这个排序的依次排序
//        if($da1['video_type']==3){
//
//        }
        $data['video_type']=1;
        $data['drop_time']=date("Y-m-d H:i:s",time());
        $data['sorts']=0;
        $res=$this->table->update("video",$data,["video_uuid"=>$video_uuid]);
        if($res){
            response(0,"弃用成功");
        }else{
            response(-1,"弃用失败");
        }
    }

    /**
     * 获取分类
     */
    public function types(){
        $biaoqian=$this->table->types();
        if($biaoqian){
            response(0,"获取成功",$biaoqian);
        }else{
            response(-1,"获取失败",$biaoqian);
        }
    }

    /**
     * 获取话题
     */
    public function type_sub(){
        $where['sub_type']=$_POST['type_id'];
        $biaoqian=$this->table->type_sub($where);
        if($biaoqian){
            response(0,"获取成功",$biaoqian);
        }else{
            response(-1,"当前分类下没有话题",$biaoqian);
        }
    }

}